<?php

class PermissaoDAO {

    public function inserir(Permissao $permissao) {
        $con = new bd();
        try {
            $con->beginTransaction();
            $stmt = $con->prepare("INSERT INTO `permissao`(`descricao`) VALUES (?);");
            $dados = Array($permissao->getDescricao());
            $stmt->execute($dados);
            $id = $con->lastInsertId();
            $con->ocorreramErros($stmt);
            $permissao->setIdPermissao($id);
            $con->commit();
        } catch (Exception $ex) {
            $con->rollBack();
        }
    }

    public function getById($id) {
        $con = new bd();
        $stmt = $con->prepare("SELECT `permissao`.`idPermissao`, `permissao`.`descricao`
                                FROM `permissao` WHERE `permissao`.`idPermissao`= ?;");
        $stmt->execute(Array($id));
        $con->ocorreramErros($stmt);

        $r = $stmt->fetch();
        return new Permissao($r[0], $r[1]);
    }

    public function listar() {
        $con = new bd();
        $stmt = $con->prepare("SELECT `permissao`.`idPermissao`, `permissao`.`descricao` FROM `permissao`;");
        $stmt->execute();
        $con->ocorreramErros($stmt);

        $permissoes = Array();
        while ($r = $stmt->fetch()) {
            $permissoes[] = new Permissao($r[0], $r[1]);
        }
        return $permissoes;
    }

}

?>
